package com.letoken.platform.app.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.letoken.platform.app.service.AppUserIssueRecordService;
import com.letoken.platform.pub.req.PageData;
import com.letoken.platform.pub.util.BigDecimalUtil;
import com.letoken.platform.repo.admin.manager.AdminIssueConfigManager;
import com.letoken.platform.repo.admin.manager.AppUserIssueRecordManager;
import com.letoken.platform.repo.admin.po.AdminIssueConfig;
import com.letoken.platform.repo.app.dao.AppUserIssueMapper;
import com.letoken.platform.repo.app.dao.AppUserIssueRecordMapper;
import com.letoken.platform.repo.app.po.AppUserIssue;
import com.letoken.platform.repo.app.po.AppUserIssueRecord;
import com.letoken.platform.repo.app.res.config.QueryAppIssueConfigRes;
import com.letoken.platform.repo.app.res.token.WaitCirculationRes;
import com.letoken.platform.repo.utlis.WebUtils;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * <p>
 * Le账户-用户铸币发行记录 服务实现类
 * </p>
 *
 * @author 作者
 * @since 2022-12-09
 */
@Service
@Log4j2
public class AppUserIssueRecordServiceImpl extends ServiceImpl<AppUserIssueRecordMapper, AppUserIssueRecord> implements AppUserIssueRecordService {

    @Autowired
    private AppUserIssueRecordManager issueRecordManager;

    @Autowired
    private AdminIssueConfigManager adminIssueConfigManager;

    @Autowired
    private AppUserIssueMapper appUserIssueMapper;

   /* @Resource
    private AdminIssueConfigManager adminIssueConfigManager;*/


    /**
     * 铸币发行-查询周期流通量
     * key为queryWaitCirculation  在配置中设置过期时间为10秒
     *
     * @return
     */
    //@Cacheable(cacheNames = "queryWaitCirculation", key = "'user_' + #userId")
    @Override
    public List<WaitCirculationRes> queryWaitCirculation(Integer userId) {
        AppUserIssue appUserIssue = appUserIssueMapper.selectById(userId);
        //根据用户id查询正在流通的流通订单 根据时间正序排序
        List<AppUserIssueRecord> issueRecordList = issueRecordManager.queryIssueRecordListByUserId(userId);
        if (CollUtil.isEmpty(issueRecordList)) {
            return null;
        }
        LocalDateTime oneRoundTime = appUserIssue.getRewardTime().plusMinutes(appUserIssue.getCountDown());
        List<WaitCirculationRes> waitCirculationResList = new ArrayList<>();
        QueryAppIssueConfigRes appIssueConfigRes = adminIssueConfigManager.queryLetokenIssueConfig();
       // LocalDateTime endTime = LocalDateTime.of(2023, 06, 01, 00, 00, 00);
        for (AppUserIssueRecord appUserIssueRecord : issueRecordList) {
            BigDecimal ss = new BigDecimal(appIssueConfigRes.getCirculateRate()).divide(new BigDecimal("100"));
            //每一期流通量
            BigDecimal amount = appUserIssueRecord.getIssueAmount().multiply(ss);
           /* if (appUserIssueRecord.getCreateTime().isBefore(endTime)) {
                amount = appUserIssueRecord.getIssueAmount().multiply(new BigDecimal("0.05"));
            }*/
            //获取一共期数
            BigDecimal bigDecimal = appUserIssueRecord.getWaitCirculation().divide(amount, RoundingMode.HALF_EVEN).setScale(0, RoundingMode.UP);
            int record = bigDecimal.intValue();
            for (int i = 0; i < record; i++) {
                WaitCirculationRes waitCirculationRes = new WaitCirculationRes();
                waitCirculationRes.setRound(i + 1);
                waitCirculationRes.setDateTime(oneRoundTime);
//                if (waitCirculationResList.size() >= record) {
                if (waitCirculationResList.size() > i) {
                    waitCirculationResList.get(i).setLetNum(waitCirculationResList.get(i).getLetNum().add(amount));
                } else {
                    waitCirculationRes.setLetNum(amount);
                    waitCirculationResList.add(waitCirculationRes);
                }
                oneRoundTime = oneRoundTime.plusDays(appIssueConfigRes.getCirculateDuration());
                //  oneRoundTime = oneRoundTime.plusMinutes(appIssueConfigRes.getCirculateDuration());
            }
        }
        log.info("查询周期流通量,用户id：{} , 周期流通量：{}",userId, JSONObject.toJSON(waitCirculationResList));
        return waitCirculationResList;
    }
}
